package com.ruoyi.material.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.GetLoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.material.mapper.MateriaPurchaseRecordMapper;
import com.ruoyi.material.domain.MateriaPurchaseRecord;
import com.ruoyi.material.service.IMateriaPurchaseRecordService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

/**
 * 材料采购记录Service业务层处理
 * 
 * @author liboyu
 * @date 2024-06-16
 */
@Service
public class MateriaPurchaseRecordServiceImpl implements IMateriaPurchaseRecordService 
{
    @Autowired
    private MateriaPurchaseRecordMapper materiaPurchaseRecordMapper;

    /**
     * 查询材料采购记录
     * 
     * @param purchaseRecordId 材料采购记录主键
     * @return 材料采购记录
     */
    @Override
    public MateriaPurchaseRecord selectMateriaPurchaseRecordByPurchaseRecordId(Long purchaseRecordId)
    {
        return materiaPurchaseRecordMapper.selectMateriaPurchaseRecordByPurchaseRecordId(purchaseRecordId);
    }

    /**
     * 查询材料采购记录列表
     * 
     * @param materiaPurchaseRecord 材料采购记录
     * @return 材料采购记录
     */
    @Override
    public List<MateriaPurchaseRecord> selectMateriaPurchaseRecordList(MateriaPurchaseRecord materiaPurchaseRecord)
    {
        materiaPurchaseRecord.setUserId(GetLoginUser.id());
        return materiaPurchaseRecordMapper.selectMateriaPurchaseRecordList(materiaPurchaseRecord);
    }

    /**
     * 新增材料采购记录
     * 
     * @param materiaPurchaseRecord 材料采购记录
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {Exception.class,Error.class})
    public int insertMateriaPurchaseRecord(MateriaPurchaseRecord materiaPurchaseRecord)
    {
        // 采购时间
        materiaPurchaseRecord.setPurchaseDatetime(DateUtils.getNowDate());
        int i = materiaPurchaseRecordMapper.insertMateriaPurchaseRecord(materiaPurchaseRecord);

        materiaPurchaseRecord.setAlreadyQuantityBefore(0);
        int i1 = materiaPurchaseRecordMapper.updateAlreadyQuantity(materiaPurchaseRecord);

        return i;
    }

    /**
     * 修改材料采购记录
     * 
     * @param materiaPurchaseRecord 材料采购记录
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {Exception.class,Error.class})
    public int updateMateriaPurchaseRecord(MateriaPurchaseRecord materiaPurchaseRecord)
    {
        int i = materiaPurchaseRecordMapper.updateMateriaPurchaseRecord(materiaPurchaseRecord);

        // 修改材料数量中的采购总计数量
        if(materiaPurchaseRecord.getAlreadyQuantityBefore() != null){
            int i1 = materiaPurchaseRecordMapper.updateAlreadyQuantity(materiaPurchaseRecord);
        }
        return i;
    }

    /**
     * 批量删除材料采购记录
     * 
     * @param purchaseRecordIds 需要删除的材料采购记录主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {Exception.class,Error.class})
    public int deleteMateriaPurchaseRecordByPurchaseRecordIds(Long[] purchaseRecordIds)
    {
        return materiaPurchaseRecordMapper.deleteMateriaPurchaseRecordByPurchaseRecordIds(purchaseRecordIds);
    }

    /**
     * 删除材料采购记录信息
     * 
     * @param purchaseRecordId 材料采购记录主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {Exception.class,Error.class})
    public int deleteMateriaPurchaseRecordByPurchaseRecordId(Long purchaseRecordId)
    {
        return materiaPurchaseRecordMapper.deleteMateriaPurchaseRecordByPurchaseRecordId(purchaseRecordId);
    }

    @Override
    public List<Map<String, Object>> selectProcurementByProjectId(Long projectId) {
        return materiaPurchaseRecordMapper.selectProcurementByProjectId(projectId);
    }
}
